<script setup lang="ts">
import { setRemind } from "@/api/user";
import { setDisplay, setGoldBasis } from "@/api/setting";
import { useTheme } from "@/composables/useTheme";
import { JumpPath } from "@/utils/index";
import { onLoad, onReady, onShow } from "@dcloudio/uni-app";
import Cache from "@/utils/cache";
import { USER_INFO } from "@/config/cache";
import { hidePageNavInWechatBrowser } from "@/composables/useHiddenNavbar";
import hideWechatMenu from "@/composables/hideWechatMenu";
import saveUserInfoToLocation from "@/composables/saveUserInfoToLocation";
onShow(() => {
  hideWechatMenu();
});
onReady(() => {
  hidePageNavInWechatBrowser();
});
let userInfo = reactive({});
onLoad(() => {
  try {
    const dataInfo = Cache.get(USER_INFO, true);
    userInfo = dataInfo;
    nameSwitch.value = !!dataInfo.enterprise_name_show;
    priceSwitch.value = !!dataInfo.high_low_show;
    titleSwitch.value = !!dataInfo.is_market_title;
    basisSwitch.value = !!+dataInfo.is_gold_basis;
    tipSwitch.value = !!+dataInfo.is_money_remind;
  } catch (error) {
    console.log(error, "error");
  }
});
const { colorStyle } = useTheme();
let nameSwitch = ref(true);
function switch1Change(e: any) {
  saveHandler();
  console.log("switch1 发生 change 事件，携带值为", nameSwitch.value);
}

// 行情标题start-------
let titleSwitch = ref(true);
function switch3Change(e: any) {
  saveHandler();
}
// 行情标题end-------
let priceSwitch = ref(true);
function switch2Change(e: any) {
  saveHandler();
  console.log("switch1 发生 change 事件，携带值为", priceSwitch.value);
}
function saveHandler() {
  const params = {
    enterprise_name_show: nameSwitch.value ? 1 : 0,
    high_low_show: priceSwitch.value ? 1 : 0,
    is_market_title: titleSwitch.value ? 1 : 0,
  };
  setDisplay(params);
}
// 基差显示start-------
let basisSwitch = ref(true);
function switch4Change(e: any) {
  const params = { is_gold_basis: e ? 1 : 0 };
  setGoldBasis(params).then((res) => {
    saveUserInfoToLocation(res.data.business);
  });
}
// 基差显示end-------

// 到价提醒start-------
let tipSwitch = ref(true);
// 到价提醒接口
function setPriceRemind() {
  const params = {
    is_money_remind: tipSwitch.value ? 1 : 0,
  };
  setRemind(params)
    .then((res: any) => {
      saveUserInfoToLocation(res.data.business);

      console.log(res, "到价提醒");
    })
    .catch(() => {
      tipSwitch.value = !!+userInfo.is_money_remind;
    });
}
function tipChange() {
  setPriceRemind();
}
// 到价提醒end-------
</script>
<template>
  <view class="wrap" :style="colorStyle">
    <!-- <view
      class="set_item mgt16 flex_sb"
      @click="JumpPath('/pages/basicSet/showSetSon/swiperList')"
    >
      <text class="blod_class">轮播图设置</text>
      <text class="iconfont icon-xiangyou"></text>
    </view> -->
    <view class="set_item mgt20 border flex_sb">
      <text class="blod_class">行情标题</text>
      <up-switch
        inactiveColor="rgb(178, 189, 194)"
        activeColor="var(--view-theme)"
        size="20"
        v-model="titleSwitch"
        @change="switch3Change"
      ></up-switch>
    </view>
    <view class="set_item mgt20 border flex_sb">
      <text class="blod_class">企业名称</text>
      <up-switch
        inactiveColor="rgb(178, 189, 194)"
        activeColor="var(--view-theme)"
        size="20"
        v-model="nameSwitch"
        @change="switch1Change"
      ></up-switch>
    </view>
    <view class="set_item mgt16 flex_sb">
      <text class="blod_class">最高最低价格</text>
      <up-switch
        inactiveColor="rgb(178, 189, 194)"
        activeColor="var(--view-theme)"
        size="20"
        v-model="priceSwitch"
        @change="switch2Change"
      ></up-switch>
    </view>
    <view class="set_item mgt16 flex_sb">
      <text class="blod_class">基差显示</text>
      <up-switch
        inactiveColor="rgb(178, 189, 194)"
        activeColor="var(--view-theme)"
        size="20"
        v-model="basisSwitch"
        @change="switch4Change"
      ></up-switch>
    </view>
    <view class="set_item mgt16">
      <view class="in_item flex_sb">
        <text class="blod_class">到价提醒</text>
        <up-switch
          inactiveColor="rgb(178, 189, 194)"
          activeColor="var(--view-theme)"
          size="20"
          v-model="tipSwitch"
          @change="tipChange"
        ></up-switch>
      </view>
      <view class="tip_text">
        开启后可在你的报价首页设置到价提醒，报价到达用户设置的价格时，平台将自动微信提醒用户
      </view>
    </view>

    <!-- <view
      class="set_item flex_sb"
      @click="JumpPath('/pages/basicSet/showSetSon/announcement')"
    >
      <text>企业声明</text>
      <text class="iconfont icon-xiangyou"></text>
    </view> -->
  </view>
</template>

<style scoped lang="scss">
.wrap {
  .mgt16 {
    margin-bottom: 16rpx;
  }
  .mgt2 {
    margin-bottom: 2rpx;
  }

  .set_item {
    background: #fff;
    padding: 40rpx;
    color: rgba(39, 38, 54, 1);
    font-size: 30rpx;
    .in_item {
      margin-bottom: 20rpx;
    }
    .tip_text {
      font-size: 28rpx;
      color: rgba(125, 136, 140, 1);
    }
    .iconfont {
      font-weight: bold;
      font-size: 30rpx;
    }
  }
}
</style>
